#include "pagecurrentevents.h"
#include "ui_pagecurrentevents.h"

PageCurrentEvents::PageCurrentEvents(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageCurrentEvents)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    InitTable();
}

PageCurrentEvents::~PageCurrentEvents()
{
    delete ui;
}

void PageCurrentEvents::InitTable()
{
    m_pCurEventTable = new QTableWidget(this);
    m_pCurEventTable->setGeometry(45,90,1000,600);
    m_pCurEventTable->setStyleSheet("background-color:black;"
                                   "selection-background-color:rgb(215,215,215);"
                                   "selection-color:black;"
                                   "color:white;"
                                   "font-size:15pt;"
                                   "gridline-color:white");
    m_pCurEventTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pCurEventTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pCurEventTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    m_pCurEventTable->setSelectionMode(QAbstractItemView::SingleSelection);
    m_pCurEventTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pCurEventTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_pCurEventTable->verticalHeader()->setVisible(false);
    m_pCurEventTable->setRowCount(15);
    m_pCurEventTable->setColumnCount(5);
    QStringList header = QStringList() << "车号" << "故障名称" << "发生时间" << "代码" << "等级";
    QString headerStyle = "QHeaderView::section{ background-color:black; color:white; height:44; font-size:16pt; border: 1px solid white }";
    m_pCurEventTable->setHorizontalHeaderLabels(header);
    m_pCurEventTable->horizontalHeader()->setStyleSheet(headerStyle);
    m_pCurEventTable->horizontalHeader()->setDisabled(true);
    m_pCurEventTable->horizontalHeader()->setFont(QFont("arial"));
    m_pCurEventTable->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Fixed);
    m_pCurEventTable->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Fixed);
    m_pCurEventTable->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Fixed);
    m_pCurEventTable->horizontalHeader()->setSectionResizeMode(3,QHeaderView::Fixed);
    m_pCurEventTable->horizontalHeader()->setSectionResizeMode(4,QHeaderView::Stretch);
    m_pCurEventTable->setFont(QFont("arial"));
    m_pCurEventTable->setColumnWidth(0, 70);
    m_pCurEventTable->setColumnWidth(1, 500);
    m_pCurEventTable->setColumnWidth(2, 200);
    m_pCurEventTable->setColumnWidth(3, 80);
    m_pCurEventTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    for(int i=0; i<m_pCurEventTable->rowCount(); ++i)
    {
        for(int j=0; j<m_pCurEventTable->columnCount(); ++j)
        {
            QTableWidgetItem *item = new QTableWidgetItem(QString::fromUtf8(""));
            if(j != 1)
                item->setTextAlignment(Qt::AlignCenter);
            item->setText("");
            m_pCurEventTable->setItem(i, j, item);
        }
    }
    connect(m_pCurEventTable, SIGNAL(itemClicked(QTableWidgetItem*)), SLOT(slotItemClicked(QTableWidgetItem*)));
    ui->widgetSolution->hide();
}

void PageCurrentEvents::updateEvent(bool xNew)
{
    int count = GlobalConfig::eventData.size();
    int rowCount = m_pCurEventTable->rowCount();
    int page = (count%rowCount) ? (count/rowCount+1) : (count/rowCount);
    ui->totalPage->setText(QString().setNum(page));
    if(ui->currentPage->text().toInt() == 0 && page > 0)
        ui->currentPage->setText("1");
    else if(ui->currentPage->text().toInt() > page)
        ui->currentPage->setText(QString().setNum(page));
    if(xNew)
        ui->currentPage->setText("1");
    else if(xNewEvent)
    {
        xNewEvent = false;
        if(ui->currentPage->text().toInt() > 1)
            ui->currentPage->setText("1");
    }
    ui->eventCount->setText(QString().setNum(count));

    updateTable();
}

void PageCurrentEvents::setNewEvent(bool xNew)
{
    xNewEvent = xNew;
}

void PageCurrentEvents::updateTable()
{
    int page = ui->currentPage->text().toInt();
    if(page == 0)
    {
        for(int i=0; i<m_pCurEventTable->rowCount(); ++i)
            for(int j=0; j<m_pCurEventTable->columnCount(); ++j)
                m_pCurEventTable->item(i, j)->setText("");
        refreshHint();
        return;
    }
    for(int i=0; i<m_pCurEventTable->rowCount(); ++i)
    {
        EventData data;
        int index = ui->eventCount->text().toInt() - 1 - i - (page-1)*m_pCurEventTable->rowCount();
        if(index < 0)
            for(int j=0; j<m_pCurEventTable->columnCount(); ++j)
                m_pCurEventTable->item(i, j)->setText("");
        else if(GlobalConfig::getEventData(index, data) == 0)
        {
            m_pCurEventTable->item(i, 0)->setText(data.event.car);
            m_pCurEventTable->item(i, 1)->setText(data.event.description);
            QString str = QDateTime::fromTime_t(data.startTime).toString("yy-MM-dd hh:mm:ss");
            m_pCurEventTable->item(i, 2)->setText(str);
            m_pCurEventTable->item(i, 3)->setText(data.event.code);
            m_pCurEventTable->item(i, 4)->setText(data.event.level);
        }
    }
}

void PageCurrentEvents::refreshHint()
{
    int row = m_pCurEventTable->currentRow();
    if(row < 0)
    {
        return;
    }
    int page = ui->currentPage->text().toInt();
    int index = ui->eventCount->text().toInt() - 1 - row - (page-1)*m_pCurEventTable->rowCount();
    if(index < 0)
    {
        return;
    }
    QString str = QString();
    EventData data;
    if(GlobalConfig::getEventData(index, data) == 0)
    {
        str = data.event.hint;
        str.replace("；", "\n");
    }
    ui->LableSolution->setText(str);
}

void PageCurrentEvents::on_btnPrev_clicked()
{
    int curpage = ui->currentPage->text().toInt();
    if(curpage > 1)
    {
        ui->currentPage->setText(QString().setNum(--curpage));
        updateTable();
    }
}

void PageCurrentEvents::on_btnNext_clicked()
{
    int curpage = ui->currentPage->text().toInt();
    int totalpage = ui->totalPage->text().toInt();
    if(curpage < totalpage)
    {
        ui->currentPage->setText(QString().setNum(++curpage));
        updateTable();
    }
}

void PageCurrentEvents::showCurEventHint()
{
    ui->widgetSolution->show();
    m_pCurEventTable->hide();
}

void PageCurrentEvents::on_btnBack_clicked()
{
    ui->widgetSolution->hide();
    m_pCurEventTable->show();
}

void PageCurrentEvents::slotItemClicked(QTableWidgetItem*)
{
    if(m_pCurEventTable->selectedItems().isEmpty())
        return;
    int row = m_pCurEventTable->currentRow();   //获取当前点击的是哪一行
    ui->CurFaultCar->setText(m_pCurEventTable->item(row,0)->text()+"车");
    ui->CurFaultDescription->setText(m_pCurEventTable->item(row,1)->text());
    ui->CurFaultLevel->setText(m_pCurEventTable->item(row,4)->text());
    ui->CurFaultCode->setText(m_pCurEventTable->item(row,3)->text());
    ui->CurFaultStartTime->setText(m_pCurEventTable->item(row,2)->text());
    refreshHint();
}

void PageCurrentEvents::hideHintInfo()
{
    ui->widgetSolution->hide();
    m_pCurEventTable->show();
}
